package org.hot100_6.leetcode42;

public class Solution {
    public int trap(int[] height) {
        int n = height.length;

        int result = 0;

        for (int i = 1; i < n-1; i++) {
            int leftMaxHeight = 0;
            //左侧最大值

            for (int j = 0; j < i; j++) {
//                leftMaxHeight = Math.max(leftMaxHeight,height[j]);
                if (height[j]>leftMaxHeight){
                    leftMaxHeight = height[j];
                }
            }

            //右侧最大值
            int rightMaxHeight = 0;
            for (int j = i+1; j <n; j++) {
//                rightMaxHeight = Math.max(rightMaxHeight,height[j]);
                if (height[j]>rightMaxHeight){
                    rightMaxHeight = height[j];
                }
            }

            int min = Math.min(leftMaxHeight, rightMaxHeight);
            if (min>height[i]){
                result+=min-height[i];
            }
        }

        return result;
    }
}
